package cn.tjd.smart_health.provider.service;

import cn.tjd.smart_health.api.CheckGroupService;
import cn.tjd.smart_health.common.entity.CheckGroup;
import cn.tjd.smart_health.common.pojo.PageSelect;
import cn.tjd.smart_health.provider.mapper.CheckGroupMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboService;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


@DubboService
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupMapper checkGroupMapper;

    /**
     *
     * @param currentPage 当前页码
     * @param pageSize  页面大小
     * @param queryString 查询条件
     * @return 查询结果
     */
    @Override
    public PageSelect<CheckGroup> findPage(Integer currentPage, Integer pageSize, String queryString) {

        LambdaQueryWrapper<CheckGroup> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(queryString != null ,CheckGroup::getName,queryString)
                .or().like(queryString != null ,CheckGroup::getCode,queryString)
                .or().like(queryString != null ,CheckGroup::getHelpCode,queryString);
        Page<CheckGroup> page = new Page<>(currentPage,pageSize);
        checkGroupMapper.selectPage(page,queryWrapper);
        PageSelect<CheckGroup> pageSelect = new PageSelect<>();
        pageSelect.setRows(page.getRecords());
        pageSelect.setTotal(page.getTotal());
        return pageSelect;
    }

    @Override
    public List<CheckGroup> findAll() {
       List<CheckGroup> data = checkGroupMapper.selectList(null);
       return data;
    }

    @Override
    public Integer addCheckGroup(CheckGroup checkGroup, Integer[] checkItems) {

        Integer integer = checkGroupMapper.insert(checkGroup);

        for (Integer checkItemId : checkItems ) {
            checkGroupMapper.insertCheckGroupAndCheckItem(checkGroup.getId(),checkItemId);
        }
        return integer;
    }

    @Override
    public Integer deleteCheckGroup(Integer id) {
        Integer result = checkGroupMapper.deleteById(id);
        result=checkGroupMapper.deleteCheckGroupAndCheckItem(id);
        return result;
    }

    @Override
    public CheckGroup findById(Integer id) {
        CheckGroup checkGroup = checkGroupMapper.selectById(id);
        return checkGroup;
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer checkGroupId) {
        List<Integer> checkItemIds = checkGroupMapper.getCheckItems(checkGroupId);
        return checkItemIds;
    }

    @Override
    public Integer updateCheckGroup(CheckGroup checkGroup, Integer[] checkItemIds) {
        Integer  result=checkGroupMapper.updateById(checkGroup);

        checkGroupMapper.deleteCheckGroupAndCheckItem(checkGroup.getId());

        for (Integer checkItemId : checkItemIds ) {
            checkGroupMapper.insertCheckGroupAndCheckItem(checkGroup.getId(), checkItemId);
        }
        return result;
    }

}
